
/* Includes ------------------------------------------------------------------*/
#include "gpio.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
gpio_struct GPIOINSignals[] =
{
	{RCC_AHBPeriph_GPIOA, GPIOA, GPIO_Pin_4,  1}, //SW1_HYDROPUMP
	{RCC_AHBPeriph_GPIOE, GPIOE, GPIO_Pin_7,  1}, //SW2_WATERPUMP
	{RCC_AHBPeriph_GPIOB, GPIOB, GPIO_Pin_13, 1}, //SW3_LIGHT
	{RCC_AHBPeriph_GPIOB, GPIOB, GPIO_Pin_14, 1}, //SW4_CAMERA

	{RCC_AHBPeriph_GPIOB, GPIOB, GPIO_Pin_15, 1}, //GRP3_GREEN
	{RCC_AHBPeriph_GPIOD, GPIOD, GPIO_Pin_11, 1}, //GRP2_RED
	{RCC_AHBPeriph_GPIOF, GPIOF, GPIO_Pin_6,  1}, //GRP1_YELLOW

	{RCC_AHBPeriph_GPIOC, GPIOC, GPIO_Pin_10, 1}, //SW_DRILL_FWD
	{RCC_AHBPeriph_GPIOC, GPIOC, GPIO_Pin_11, 1}, //SW_DRILL_RWD
	{RCC_AHBPeriph_GPIOC, GPIOC, GPIO_Pin_12, 1}, //SW_CAMERA_REC
	{RCC_AHBPeriph_GPIOD, GPIOD, GPIO_Pin_0,  1}, //SW_ROTARY3
	{RCC_AHBPeriph_GPIOD, GPIOD, GPIO_Pin_1,  1}  //SW_ROTARY4

};

gpio_struct GPIOOUTSignals[] =
{
	{RCC_AHBPeriph_GPIOD, GPIOD, GPIO_Pin_2,  0}, //SW1_HYDROPUMP_LED
	{RCC_AHBPeriph_GPIOD, GPIOD, GPIO_Pin_5,  0}, //SW2_WATERPUMP_LED
	{RCC_AHBPeriph_GPIOB, GPIOB, GPIO_Pin_8,  0}, //SW3_LIGHT_LED
	{RCC_AHBPeriph_GPIOB, GPIOB, GPIO_Pin_9,  0}, //SW4_CAMERA_LED

	{RCC_AHBPeriph_GPIOE, GPIOE, GPIO_Pin_6,  0}, //GRP3_GREEN_LED
	{RCC_AHBPeriph_GPIOC, GPIOC, GPIO_Pin_13, 0}, //GRP2_RED_LED
	{RCC_AHBPeriph_GPIOC, GPIOC, GPIO_Pin_14, 0}, //GRP1_YELLOW_LED

	{RCC_AHBPeriph_GPIOF, GPIOF, GPIO_Pin_9,  0}  //POWER_LED
};

/* Private functions ---------------------------------------------------------*/

void GPIO_Config(void)
{
	uint8_t i;
	GPIO_InitTypeDef GPIO_InitStructure;

	// Setup inputs
	
	GPIO_StructInit(&GPIO_InitStructure);
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
 	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	
	for (i = 0; i < COUNT_OF(GPIOINSignals); i++)
	{
		/* GPIOx Periph clock enable */
		RCC_AHBPeriphClockCmd(GPIOINSignals[i].RCC_AHBPeriph, ENABLE);
		GPIO_InitStructure.GPIO_Pin = GPIOINSignals[i].GPIO_Pin;
		
		GPIO_Init(GPIOINSignals[i].GPIO_Port, &GPIO_InitStructure);
	}

	// Setup outputs

	GPIO_StructInit(&GPIO_InitStructure);
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
 	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
 	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

	for (i = 0; i < COUNT_OF(GPIOOUTSignals); i++)
	{
		/* GPIOx Periph clock enable */
		RCC_AHBPeriphClockCmd(GPIOOUTSignals[i].RCC_AHBPeriph, ENABLE);
		GPIO_InitStructure.GPIO_Pin = GPIOOUTSignals[i].GPIO_Pin;
		
		GPIO_Init(GPIOOUTSignals[i].GPIO_Port, &GPIO_InitStructure);
		
		if (GPIOOUTSignals[i].pull_drive == 1)
		{
			GPIO_SetBits(GPIOOUTSignals[i].GPIO_Port, GPIOOUTSignals[i].GPIO_Pin);
		}
		else
		{
			GPIO_ResetBits(GPIOOUTSignals[i].GPIO_Port, GPIOOUTSignals[i].GPIO_Pin);
		}
	}
	
}

uint8_t GPIO_GetInputState(GPIOINSignals_enum in_signal)
{
	return GPIO_ReadInputDataBit(GPIOINSignals[in_signal].GPIO_Port, GPIOINSignals[in_signal].GPIO_Pin) ? 0 : 1;
}

void GPIO_SetOutput(GPIOOUTSignals_enum out_signal, uint8_t state)
{
	if (state)
	{
		GPIO_SetBits(GPIOOUTSignals[out_signal].GPIO_Port, GPIOOUTSignals[out_signal].GPIO_Pin);
	}
	else
	{
		GPIO_ResetBits(GPIOOUTSignals[out_signal].GPIO_Port, GPIOOUTSignals[out_signal].GPIO_Pin);
	}
}

void GPIO_Task(void)
{
	
}
